package invokerV3;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;

import command.Command;

import invoker.Ihm;

public class IhmImplV3 implements Observer, Ihm, ActionListener{

	private JFrame miniEditeurV3;

	private int debutSelection;

	private int finSelection;

	private String textInserer = new String();

	private JTextArea textAreaInserer;

	private JTextArea textAreaContenu;

	private JPanel panelBoutons;

	private JScrollPane scrollPaneInserer;

	private JScrollPane scrollPaneContenuBuffer;

	private JButton btnCouper;

	private JButton btnColler;

	private JButton btnCopier;
	private JButton btnRejouer;
	private JButton btnArrter;
	private JButton btnDmarrer;
	private JPanel panelBoutonsV1;
	private JButton btnDefaire;
	private JButton btnRefaire;

	private HashMap<String, Command> _lesCommandes;

	private JButton btnSaisir;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					IhmImplV3 window = new IhmImplV3();
					window.miniEditeurV3.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public IhmImplV3() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		miniEditeurV3 = new JFrame();
		miniEditeurV3.setTitle("Version finale du mini-éditeur");
		miniEditeurV3.setBounds(100, 100, 562, 353);
		miniEditeurV3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		panelBoutons = new JPanel();

		scrollPaneInserer = new JScrollPane();

		scrollPaneContenuBuffer = new JScrollPane();

		panelBoutonsV1 = new JPanel();

		JPanel panel = new JPanel();

		btnDefaire = new JButton("Défaire");
		panel.add(btnDefaire);
		btnDefaire.addActionListener(this);

		btnRefaire = new JButton("Refaire");
		panel.add(btnRefaire);
		btnRefaire.addActionListener(this);

		GroupLayout groupLayout = new GroupLayout(miniEditeurV3.getContentPane());
		groupLayout.setHorizontalGroup(
				groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
						.addContainerGap()
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(scrollPaneContenuBuffer, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE)
								.addComponent(scrollPaneInserer, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE)
								.addComponent(panel, GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE)
								.addComponent(panelBoutons, GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE)
								.addComponent(panelBoutonsV1, GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE))
								.addContainerGap())
				);
		groupLayout.setVerticalGroup(
				groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
						.addContainerGap()
						.addComponent(panel, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(panelBoutons, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addComponent(panelBoutonsV1, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addComponent(scrollPaneInserer, GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
						.addGap(18)
						.addComponent(scrollPaneContenuBuffer, GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)
						.addGap(10))
				);

		//		btnInsrer = new JButton("Insérer");
		//		
		//		panelBoutonsV1.add(btnInsrer);


		btnSaisir = new JButton("Saisir");
		btnSaisir.addActionListener(this);
		panelBoutonsV1.add(btnSaisir);
		
		btnCouper = new JButton("Couper");
		btnCouper.addActionListener(this);
		panelBoutonsV1.add(btnCouper);

		btnColler = new JButton("Coller");
		btnColler.addActionListener(this);
		panelBoutonsV1.add(btnColler);

		btnCopier = new JButton("Copier");
		btnCopier.addActionListener(this);
		panelBoutonsV1.add(btnCopier);

		textAreaContenu = new JTextArea();


		scrollPaneContenuBuffer.setViewportView(textAreaContenu);

		textAreaInserer = new JTextArea("");

		scrollPaneInserer.setViewportView(textAreaInserer);
//		textAreaInserer.getDocument().addDocumentListener(new MyDocumentListener());
//
//		textAreaInserer.addCaretListener(new CaretListener() {
//
//			@Override
//			public void caretUpdate(CaretEvent arg0) {
//				debutSelection = arg0.getDot();
//				finSelection = arg0.getMark();
//				_lesCommandes.get("SelectionnerOriginatorV3").execute();
//			}
//		});


		textAreaContenu.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
				debutSelection = textAreaContenu.getSelectionStart();
				finSelection = textAreaContenu.getSelectionEnd();
				_lesCommandes.get("SelectionnerOriginatorV3").execute();
			}
		});
		btnDmarrer = new JButton("Démarrer");
		btnDmarrer.addActionListener(this);
		panelBoutons.add(btnDmarrer);

		btnArrter = new JButton("Arrêter");
		btnArrter.addActionListener(this);
		panelBoutons.add(btnArrter);

		btnRejouer = new JButton("Rejouer");
		btnRejouer.addActionListener(this);
		panelBoutons.add(btnRejouer);

		miniEditeurV3.getContentPane().setLayout(groupLayout);
		miniEditeurV3.setVisible(true);
	}

	@Override
	public String getTexteInserer() {
		return textInserer;
	}

	@Override
	public int getDebutSelection() {
		return debutSelection;
	}

	@Override
	public int getFinSelection() {
		return finSelection;
	}

	@Override
	public void update(Observable o, Object arg) {
		textAreaContenu.setText((String) arg);
		//textAreaInserer.setText((String) arg);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==btnSaisir){
			textInserer = textAreaInserer.getText();
			_lesCommandes.get("SaisirOriginatorV3").execute();
			textAreaInserer.setText("");
		}
		if(e.getSource()==btnCopier){
			get_lesCommandes().get("CopierOriginatorV3").execute();
		}
		if(e.getSource()==btnColler){
			get_lesCommandes().get("CollerOriginatorV3").execute();
		}
		if(e.getSource()==btnCouper){
			get_lesCommandes().get("CouperOriginatorV3").execute();
		}
		if(e.getSource()==btnDmarrer){
			get_lesCommandes().get("Demarrer").execute();
		}
		if(e.getSource()==btnRejouer){
			get_lesCommandes().get("Rejouer").execute();
		}
		if(e.getSource()==btnArrter){
			get_lesCommandes().get("Arreter").execute();
		}
		if(e.getSource()==btnDefaire){
			textAreaContenu.setText("");
			get_lesCommandes().get("Defaire").execute();
		}
		if(e.getSource()==btnRefaire){
			get_lesCommandes().get("Refaire").execute();
		}
	}

	class MyDocumentListener implements DocumentListener {

		@Override
		public void changedUpdate(DocumentEvent e) {

		}

		@Override
		public void insertUpdate(DocumentEvent e) {
//			try {
//				String s = e.getDocument().getText(e.getOffset(), e.getLength());
//				textAreaContenu.setText (textInserer += s);
//				//				debutSelection = e.getOffset();
//				//				finSelection = debutSelection ;
//				textInserer = s;
//				//				System.out.println("deb\t"+debutSelection + "\tfin\t"+finSelection +"\tString\t"+s);
//				//				//				_lesCommandes.get("SelectionnerOriginatorV3").execute();
//
//				_lesCommandes.get("SaisirOriginatorV3").execute();
//			} catch (BadLocationException e1) {
//				// TODO Auto-generated catch block
//				e1.printStackTrace();
//			}

		}

		@Override
		public void removeUpdate(DocumentEvent e) {
//			textInserer = ""; 
//			-- debutSelection;
//			_lesCommandes.get("SelectionnerOriginatorV3").execute();
//			//			System.out.println("Début  :  "+debutSelection + "   Fin  :  " + finSelection);
//			_lesCommandes.get("SaisirOriginatorV3").execute();
		}

	}

	/**
	 * @return the _lesCommandes
	 */
	public HashMap<String, Command> get_lesCommandes() {
		return _lesCommandes;
	}

	/**
	 * @param _lesCommandes the _lesCommandes to set
	 */
	public void set_lesCommandes(HashMap<String, Command> _lesCommandes) {
		this._lesCommandes = _lesCommandes;
	}
}